"""
字符串的 查找 相关功能
"""
string = '上海自来水来自海上'

# 出现次数 count
a = string.count('海')
print(a)  # 2
# 限定查找出现次数的位置
a = string.count('海', 0, 5)
print(a)  # 1

# 找到元素的下标
x = string.find('海')  # 正序
print(x)  # 1

x = string.rfind('海')  # 倒叙
print(x)  # 7

# 以上两个方法找不到匹配元素返回-1

x = string.index('海')
print(x)  # 1

x = string.rindex('海')
print(x)  # 7

# 以上两个方法找不到报错

# x = string.rindex('君君') ValueError: substring not found

code = """  print("i love python")
    print("我是大学牲")"""
new_code = code.expandtabs(4)
print(new_code)
#   print("i love python")
#     print("我是大学牲")

# 替换
# replace(old,new)
# s = string.replace('上', '下')
# print(s)  # 下海自来水来自海下
s = string.replace('上', '下')
print(s)

# 转换
table = str.maketrans('abcdefg', '1234567')

print('I love python'.translate(table))
# I lov5 python
# 还有第三个参数将其忽略
table = str.maketrans('abcdefg', '1234567', 'I')

print('I love python'.translate(table))
#  lov5 python
